<template>
	<view class="fui-wrap">
		<fui-result v-if="error" type="fail" title="操作失败" :descr="error">	
			<fui-button @click="$reLaunch(is_merchant?'merchant/index':'index/index')" width="400rpx" height="84rpx" text="返回主页" type="success" bold :margin="['48rpx','0','24rpx']"></fui-button>
		</fui-result>
		<view class="fui-page__bd" v-if="success">
			<view class="top">
				<fui-icon name="checkbox-fill" :size="55" color="#13b03f"></fui-icon>
				<fui-text text="领券成功！" type="success" size="50" block fontWeight="800" style="margin-left: 10rpx;"></fui-text>
			</view>
			<fui-list bottomBorder title="确认停车信息：">
				<fui-list-cell>
					<view class="title">车牌号：</view>
					<view class="content"><fui-tag :text="success.plate_number" type="primary"></fui-tag></view>
				</fui-list-cell>
				<block v-if="success.records">
					<fui-list-cell>
						<view class="title">入场时间：</view>
						<view class="content">{{formatDateTime(success.records.entry_time)}}</view>
					</fui-list-cell>
					<fui-list-cell>
						<view class="title">停车费用：</view>
						<view class="content">￥{{success.records.total_fee}}</view>
					</fui-list-cell>
					<fui-list-cell v-if="success.records.pay_fee>0">
						<view class="title">提前付款：</view>
						<view class="content">￥{{success.records.pay_fee}}</view>
					</fui-list-cell>
					<fui-list-cell>
						<view class="title">优惠券：</view>
						<view class="content">{{success.couponlist.coupon.title}}</view>
					</fui-list-cell>
					<fui-list-cell>
						<view class="title">优惠金额：</view>
						<fui-text :text="'￥'+success.records.activities_fee" type="success" size="36"></fui-text>
					</fui-list-cell>
					<fui-list-cell>
						<view class="title">实付金额：</view>
						<fui-text :text="'￥'+success.records.need_pay_fee" type="danger" size="36" fontWeight="800"></fui-text>
					</fui-list-cell>
				</block>
				<block v-else>
					<fui-list-cell>
						<view class="title">优惠券：</view>
						<view class="content">{{success.couponlist.coupon.title}}</view>
					</fui-list-cell>
				</block>
			</fui-list>
			<view class="tips">
				<fui-text :text="tips[0]" type="danger" size="24"></fui-text>
				<fui-text :text="tips[1]" type="danger" size="24"></fui-text>
			</view>
			<view class="fanhui">
				<fui-button @click="$reLaunch(is_merchant?'merchant/index':'index/index')" text="返回主页" type="success" bold></fui-button>
			</view>
		</view>
		<block v-if="!error && !success">
			<block v-if="platform=='PC'">
				<view class="plate_number_box">
					<fui-input v-model="plate_number" :inputBorder="true" placeholder="请输入车牌号"></fui-input>
					<fui-button :disabledButton="disabledButton" :margin="['48rpx','0','24rpx']" @click="clickConfirm" text="确认车牌号" type="success" bold></fui-button>
				</view>
				<pcfooter></pcfooter>
			</block>
			<block v-else>
				<plateNumber v-if="parking" :value="parking.plate_begin" @confirm="confirmPlate" @cancel="cancelPlate"></plateNumber>
			</block>
		</block>
	</view>
</template>

<script>
	import {methods,baseUrl} from '@/utils/core.js';
	import plateNumber from '@/components/common/plate.vue';
	import {formatDateTime} from '@/utils/util.js';
	import pcfooter from '@/components/common/pcfooter.vue';
	export default {
		components:{
			plateNumber:plateNumber,
			pcfooter:pcfooter
		},
		data() {
			return {
				error:'',
				success:'',
				qrcode_id:'',
				is_merchant:false,
				scan:'',
				tips:['',''],
				parking:'',
				plate_number:'',
				disabledButton:false,
				platform:''
			}
		},
		onLoad(e) {
			this.qrcode_id=e.qrcode_id;
			this.$platform();
			this.init();
		},
		methods: {
			...methods,
			init:function(){
				let platform='miniapp';
				if(this.platform=='PC'){
					platform='mpapp';
				}
				this.$get('merchant/common/is-admin').then(res=>{
					this.is_merchant=res;
					this.$get('merchant/common/read-qrcode',{
						qrcode_id:this.qrcode_id,
						platform:platform
					}).then(res=>{
						this.scan=res.scan;
						this.parking=res.parking;
					}).catch(err=>{
						this.error=err.msg;
					});
				});
			},
			cancelPlate:function(){
				let url=this.is_merchant?'merchant/index':'index/index'
				this.$reLaunch(url);
			},
			formatDateTime:function(i){
				return formatDateTime(new Date(i*1000));
			},
			clickConfirm:function(){
				this.disabledButton=true;
				this.$post('common/plate-number',{plate_number:this.plate_number},true,true).then(res=>{
					this.confirmPlate(this.plate_number);
				}).catch(err=>{
					this.disabledButton=false;
				});
			},
			confirmPlate:function(plate_number){
				this.$post('merchant/common/push-result',{
					plate_number:plate_number,
					scan_id:this.scan.id,
				},true).then(res=>{
					let expiretime=this.formatDateTime(res.couponlist.expiretime);
					this.tips[0]='注：';
					if(res.records){
						this.tips[0]+='停车费用为截至到当前时间的费用！';
					}
					if(expiretime!='2099-12-31 23:59:59'){
						if(this.tips[0].length>10){
							this.tips[1]="优惠券到期时间为"+expiretime+"，请务必在优惠券到期时间结束前出场！";
						}else{
							this.tips[0]+="优惠券到期时间为"+expiretime+"，请务必在优惠券到期时间结束前出场！";
						}
					}else{
						this.tips[0]+='永久有效！';
					}
					this.success=res;
				}).catch(err=>{
					this.error=err.msg;
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.fui-wrap {
		background-color: #fff;
		padding-bottom: 40rpx;
	}
	.fui-page__bd{
		.top{
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 20rpx 0;
		}
	}
	.tips{
		padding: 30rpx 24rpx;
	}
	.fanhui{
		padding: 20rpx 24rpx;
	}
	.plate_number_box{
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 300rpx;
	}
</style>